/*
 * Duplicate Detector Copyright (C) 2010 Marco Biscaro <marcobiscaro2112@gmail.com>
 * 
 * This file is part of Duplicate Detector.
 *
 * Duplicate Detector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Duplicate Detector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Duplicate Detector.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.duplicatedetector.controller;

import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.googlecode.duplicatedetector.properties.PropertiesManager;
import com.googlecode.duplicatedetector.util.Logger;
import com.googlecode.duplicatedetector.view.LookAndFeels;

/**
 * Changes the {@link LookAndFeel}.
 * 
 * @author Marco Biscaro
 */
public class ChangeLaFActionListener implements ActionListener {

	private static final Logger LOGGER = new Logger(
			ChangeLaFActionListener.class);
	private Component comp;
	private LookAndFeel laf;

	public ChangeLaFActionListener(Component comp, LookAndFeel laf) {
		this.comp = comp;
		this.laf = laf;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (UIManager.getLookAndFeel().equals(laf)) {
			return;
		}
		Window parent = SwingUtilities.getWindowAncestor(comp);
		LOGGER.debug("Changing look and feel to " + laf);
		try {
			UIManager.setLookAndFeel(laf);
			SwingUtilities.updateComponentTreeUI(parent);
			parent.pack();
			PropertiesManager.setProperty("laf", LookAndFeels.getName(laf));
		} catch (UnsupportedLookAndFeelException e1) {
			LOGGER.exception("Error setting look and feel", e1);
		}

	}

}
